<?php
/*
Aurus CMS - free OOP CMS
Copyright (C) 2010  Nestor Yanchuk

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
if (! defined('ROOT')) die();

if (isset($_GET['gid'])) {
	if (isset($_POST['data'])) {
		Cache::clear('usergroups');
		Mysql::getInstance()->query("UPDATE ".PREFIX."_usergroups SET groupallow='".serialize($_POST['data'])."' WHERE gid='{$_GET['gid']}'");
		Aurus::getInstance()->addContent( Adminarea::getInstance()->adminInfo(__('Зміни збережено'),0,LINK_ROOT.'adminarea/?do=users&amp;mod=groups',1));
	} else {
		$group_id = $_GET['gid'];
		$tr = '';
		$td = '';
		foreach (Users::getInstance()->USERGROUPDATA as $name => $arrs){
			list($desc,$fieldtype,$vars) = $arrs;
			switch ($fieldtype) {
				case 'input':
					$td = Html::td($desc,'confclass');
					$td .= Html::td('<input name="data['.$name.']" type="text" value="'.Users::getInstance()->getGroupData($name,$group_id).'" />','confclass');
					$tr .= Html::tr($td);
					break;
				case 'textarea':
					$td = Html::td($desc,'confclass');
					$td .= Html::td('<textarea name="data['.$name.']" style="width:300px;height:150px;">'.Users::getInstance()->getGroupData($name,$group_id).'</textarea>','confclass');
					$tr .= Html::tr($td);
					break;
				case 'select':
					$def = Users::getInstance()->getGroupData($name,$group_id);
					$ret = '<select name="data['.$name.']">';
					if ((gettype($vars) !== gettype(array())) or !count($vars) ) {
						$ret .= '<option value=""></option></select>';
					} else {
						foreach ($vars as $key => $value){
							$p = ($def == $key) ? ' selected="selected" ' : '';
							$ret .= '<option value="'.$key.'"'.$p.'>'.$value.'</option>';
						}
						$ret .= '</select>';

						$td = Html::td($desc,'confclass');
						$td .= Html::td($ret,'confclass');
						$tr .= Html::tr($td);
					}

					break;
			}
		}
		$but = '<input type="submit" value="'.__('Змінити').'" />';
		Aurus::getInstance()->addContent( Aurus::getInstance()->form_start().Html::table($tr).$but.'</form>');
	}
} elseif (isset($_GET['deletegroup'])){
	Cache::clear('usergroups');
	Mysql::getInstance()->delete('usergroups','gid='.$_GET['deletegroup']);
	Aurus::getInstance()->addContent( Adminarea::getInstance()->adminInfo(__('Групу видалено')));

} elseif (isset($_GET['all']) && $_GET['all'] === 'true'){
	if (isset($_POST['data'])) {
		Cache::clear('usergroups');
		
		foreach ($_POST['data'] as $id => $data) {
			Mysql::getInstance()->query("UPDATE ".PREFIX."_usergroups SET groupallow='".serialize($data)."' WHERE gid='{$id}'");
		}

		Aurus::getInstance()->addContent( Adminarea::getInstance()->adminInfo(__('Зміни збережено'),0,'',1));
	} else {

		$all = Mysql::getInstance()->select('usergroups');

		//make head
		$td = Html::td(__('Назва настройки'),'chead');
		foreach (Users::getInstance()->getUserGroups() as $group) {
			$td .= Html::tag($group['groupname'],'td',array('class' => 'chead'));
		}
		$tr = Html::tr($td);
		//end

		foreach (Users::getInstance()->USERGROUPDATA as $name => $arrs) {
			list($desc,$fieldtype,$vars) = $arrs;
			$td = Html::tag($desc,'td',array('class' => 'confclass'));

			foreach (Users::getInstance()->getUserGroups() as $group_id => $group) {
				switch ($fieldtype) {
					case 'input':
						$td .= Html::tag('<input name="data['.$group_id.']['.$name.']" type="text" value="'.Users::getInstance()->getGroupData($name,$group_id).'" />','td',array('class'=>'confclass'));
						break;
					case 'textarea':
						$td .= Html::tag('<textarea name="data['.$group_id.']['.$name.']" style="width:300px;height:150px;">'.Users::getInstance()->getGroupData($name,$group_id).'</textarea>','td',array('class'=>'confclass'));
						break;
					case 'select':
						$def = Users::getInstance()->getGroupData($name,$group_id);
						$ret = '<select name="data['.$group_id.']['.$name.']">';
						if ((gettype($vars) !== gettype(array())) or !count($vars) ) {
							$ret .= '<option value=""></option></select>';
						} else {
							foreach ($vars as $key => $value){
								$p = ($def == $key) ? ' selected="selected" ' : '';
								$ret .= '<option value="'.$key.'"'.$p.'>'.$value.'</option>';
							}
							$ret .= '</select>';

							$td .= Html::tag($ret,'td',array('class'=>'confclass'));
						}

						break;
				}
			}

			$tr .= Html::tr($td);
		}
		$button = '<div align="center"><input class="input" type="submit" value="'.__('Зберегти').'"/></div>';
		Aurus::getInstance()->addContent(Aurus::getInstance()->form_start().Html::table($tr).$button.'</form>');

	}
} else {
	$g = Mysql::getInstance()->query("SELECT * FROM ".PREFIX."_usergroups");
	$tr = Html::tr(
	Html::td('ID','chead','left','5%').
	Html::td(__('Група'),'chead','left','65%').
	Html::td(__('Змінити'),'chead','left','15%').
	Html::td(__('Видалити'),'chead','left','15%')
	);

	$td = '';
	while ($group = Mysql::getInstance()->fetch_assoc($g)) {
		$td = Html::td($group['gid'],'confclass');
		$td .= Html::td(Users::getInstance()->getGroupData('groupname',$group['gid']),'confclass');
		$td .= Html::td('<a href="'.LINK_ROOT.'adminarea/?do=users&amp;mod=groups&amp;gid='.$group['gid'].'">'.__('Змінити'),'confclass');

		if($group['gid'] < 5){
			$td .= Html::td(__('Видалити'),'confclass');
		} else {
			$td .= Html::td('<a onclick="return confirm(\''.__('Видалити групу?').'\');" href="'.LINK_ROOT.'adminarea/?do=users&amp;mod=groups&amp;deletegroup='.$group['gid'].'">'.__('Видалити').'</a>','confclass');
		}


		$tr .= Html::tr($td);
	}
	Aurus::getInstance()->addContent( Html::table($tr));
	Aurus::getInstance()->addContent( Html::tag(__('Всі разом'),'a',array('href' => LINK_ROOT.'adminarea/?do=users&mod=groups&all=true' ) ) );

}